package com.tjec.project.projectWorkbench.service.impl;

import java.util.List;
import java.util.UUID;

import com.tjec.common.utils.DateUtils;
import com.tjec.common.utils.SecurityUtils;
import com.tjec.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tjec.project.projectWorkbench.mapper.BmsAgencyNoMapper;
import com.tjec.project.businessWorkbench.domain.BmsBusiness;
import com.tjec.project.businessWorkbench.mapper.BmsBusinessMapper;
import com.tjec.project.projectWorkbench.domain.BmsAgencyNo;
import com.tjec.project.projectWorkbench.domain.BmsBusinessOper;
import com.tjec.project.projectWorkbench.service.IBmsAgencyNoService;
import com.tjec.project.projectWorkbench.service.IBmsBusinessOperService;

/**
 * 招标代理号Service业务层处理
 * 
 * @author pangyongfeng
 * @date 2020-07-31
 */
@Service
public class BmsAgencyNoServiceImpl implements IBmsAgencyNoService 
{
    @Autowired
    private BmsAgencyNoMapper bmsAgencyNoMapper;
    
    @Autowired
    private BmsBusinessMapper bmsBusinessMapper;
    
    @Autowired
	private IBmsBusinessOperService bmsBusinessOperService;

    /**
     * 查询招标代理号
     * 
     * @param guid 招标代理号ID
     * @return 招标代理号
     */
    @Override
    public BmsAgencyNo selectBmsAgencyNoByGuid(String guid)
    {
        return bmsAgencyNoMapper.selectBmsAgencyNoByGuid(guid);
    }

    /**
     * 查询招标代理号列表
     * 
     * @param bmsAgencyNo 招标代理号
     * @return 招标代理号
     */
    @Override
    public List<BmsAgencyNo> selectBmsAgencyNoList(BmsAgencyNo bmsAgencyNo)
    {
        return bmsAgencyNoMapper.selectBmsAgencyNoList(bmsAgencyNo);
    }

    /**
     * 新增招标代理号
     * 
     * @param bmsAgencyNo 招标代理号
     * @return 结果
     */
    @Override
    public String insertBmsAgencyNo(BmsAgencyNo bmsAgencyNo)
    {
    	if(StringUtils.isNotBlank(bmsAgencyNo.getGuid())) {
    		bmsAgencyNoMapper.updateBmsAgencyNo(bmsAgencyNo);
    	}else {
    		bmsAgencyNo.setCreateTime(DateUtils.getNowDate());
            /*if(StringUtils.isBlank(bmsAgencyNo.getCreateBy())){
                bmsAgencyNo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
            }*/
            bmsAgencyNoMapper.insertBmsAgencyNo(bmsAgencyNo);
            
            BmsBusiness oldBusiness = bmsBusinessMapper.selectBmsBusinessByGuid(bmsAgencyNo.getBusinessId());
        	if(oldBusiness!=null && StringUtils.isNotBlank(bmsAgencyNo.getAgencyNo())) {
        		//添加操作记录
            	BmsBusinessOper bmsBusinessOper = new BmsBusinessOper();
            	bmsBusinessOper.setBusinessId(oldBusiness.getGuid());
            	bmsBusinessOper.setOperPerson(SecurityUtils.getLoginUser().getUser().getNickName());
        		bmsBusinessOper.setOperPersonNo(SecurityUtils.getLoginUser().getUser().getJobNo());
        		bmsBusinessOper.setBusinessName(oldBusiness.getName());
        		bmsBusinessOper.setProjectId(oldBusiness.getProjectId());
        		bmsBusinessOper.setFieldName("报告号/招标号");
        		bmsBusinessOper.setBusinessNo(oldBusiness.getSectionNumber());
        		bmsBusinessOper.setOperType("6");
        		bmsBusinessOper.setNewParams(bmsAgencyNo.getAgencyNo());
        		bmsBusinessOperService.insertBmsBusinessOper(bmsBusinessOper);
        	}
    	}
        
        return bmsAgencyNo.getGuid();
    }

    /**
     * 修改招标代理号
     * 
     * @param bmsAgencyNo 招标代理号
     * @return 结果
     */
    @Override
    public int updateBmsAgencyNo(BmsAgencyNo bmsAgencyNo)
    {
        return bmsAgencyNoMapper.updateBmsAgencyNo(bmsAgencyNo);
    }

    /**
     * 批量删除招标代理号
     * 
     * @param guids 需要删除的招标代理号ID
     * @return 结果
     */
    @Override
    public int deleteBmsAgencyNoByGuids(String[] guids)
    {
        return bmsAgencyNoMapper.deleteBmsAgencyNoByGuids(guids);
    }

    /**
     * 删除招标代理号信息
     * 
     * @param guid 招标代理号ID
     * @return 结果
     */
    @Override
    public int deleteBmsAgencyNoByGuid(String guid)
    {
        return bmsAgencyNoMapper.deleteBmsAgencyNoByGuid(guid);
    }

	@Override
	public Integer checkBmsAgencyNo(BmsAgencyNo bmsAgencyNo) {
		return bmsAgencyNoMapper.checkBmsAgencyNo(bmsAgencyNo);
	}

    /**
     * 更新招标代理历史表状态为“已迁移”
     * @param bmsAgencyNo
     * @return
     */
    @Override
    public int updateBmsAgencyNoHistory(BmsAgencyNo bmsAgencyNo) {
        return bmsAgencyNoMapper.updateBmsAgencyNoHistory(bmsAgencyNo);
    }

    /**
     * 修改招标代理号通过招标代理号
     * @param bmsAgencyNo
     * @return
     */
    @Override
    public int updateBmsAgencyNoByAgencyNo(BmsAgencyNo bmsAgencyNo) {
        return bmsAgencyNoMapper.updateBmsAgencyNoByAgencyNo(bmsAgencyNo);
    }

    /**
     * 修改招标代理号历史表通过招标代理号
     * @param bmsAgencyNo
     * @return
     */
    @Override
    public int updateBmsAgencyNoHistoryByAgencyNo(BmsAgencyNo bmsAgencyNo) {
        return bmsAgencyNoMapper.updateBmsAgencyNoHistoryByAgencyNo(bmsAgencyNo);
    }
}
